<?php
declare (strict_types = 1);

namespace app\admin\model;


use think\Model;

class Brand extends Model{

    //获取器
    public function getRecommendedAttr($value){
        $config = \think\facade\Config::get('param.param_recommended');
        return $config[$value];
    }

    public function getIfShowAttr($value){
        $config = \think\facade\Config::get('param.param_show');
        return $config[$value];
    }

    //搜索器
    public  function searchBrandNameAttr($query,$value)
    {
        if (!''==$value){
            $query->where('brand_name','like','%'.$value.'%');
        }
    }

    public function searchIfShowAttr($query,$value)
    {
        if (isset($value) && is_scalar($value) && '' != $value){
            $query->where('if_show',strval($value));
        }
        if (isset($value) && is_array($value) && count($value) >0){
            $query->whereIn('if_show',$value);
        }

    }

    public function searchBrandLogoAttr($query,$value)
    {
        if ($value==1){
            $query->whereNotNull('brand_logo')->where('brand_logo','<>','');
        }
        if($value==2){
            $query->whereNull('brand_logo')->whereOr('brand_logo','=','');
        }

    }

    public static function getBrandList($data=[],$query=[],$page = true)
    {
        $model = self::field(true)
                ->withSearch(array_keys($data),$data);

//        if(isset($data['ifhow']) && $data['ifhow']!=""){
//            $model->where('if_show',$data['ifhow']);
//        }
//        if(isset($data['keywords']) && $data['keywords']!=""){
//            $model->whereLike('brand_name','%'.$data['keywords'].'%');
//        }
//        if (isset($data['logo']) && $data['logo']!=''){
//            if ($data['logo']==1){
//                $model->whereNotNull('brand_logo')->where('brand_logo','<>','');
//            }
//            if($data['logo']==2){
//                $model->whereNull('brand_logo')->whereOr('brand_logo','=','');
//            }
//        }
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }

    }
}